define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/order/index',
                    add_url: 'order/order/add',
                    edit_url: 'order/order/edit',
                    del_url: 'order/order/del',
                    multi_url: 'order/order/multi',
                    table: 'shop_order',
                    ajax_url:{
                        btn_1:'order/order/flag_act?type=1',//1:插旗
                        btn_8:'order/order/edit?type=3',//1:编辑
                        btn_3:{data:{'method':"1"},url:"order/order/isExamine",fc:function(data){
                                return true;
                            }},//审核
                        btn_4:{data:{'method':"2"},url:"order/order/isReject",fc:function(data){
                                return true;
                            }},//驳回
                        btn_6:{data:{'method':"4"},url:"order/order/isExpedit",fc:function(data){
                                return true;
                            }},//撤销
                        btn_7:{data:{'method':"5"},url:"order/order/isCancel",fc:function(data){
                                return true;
                            }},//回收，
                        btn_5:{data:{'method':"3"},url:"order/order/isRecovery",fc:function(data){
                                return true;
                            }},//催付
                    },
                    btn_mall_window:{
                        btn_array:["确定","取消"],
                        btn_evn_array_length:2,
                        btn_evn_array: {
                            evn_1:function (index,layero) {
                                var frame = Layer.getChildFrame('html', index);
                                var form = $("form",frame);
                                var success = function () {
                                    table.bootstrapTable('refresh');
                                    Layer.close(index);
                                }
                                Fast.api.form_ajax_submit(form,success);
                            },
                            evn_2:function (index,layero) {
                                Layer.close(index);
                            }
                        },
                        area:['600px','300px']
                    }

                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'order_id',
                sortName: 'order_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'order_sno', title: __('订单编号(订单表)'),formatter:function(value,row,index){
                                var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                                var index = keys.indexOf(value);
                                value =  value.toString() + "&nbsp;";
                                // var display = index > -1 ? this.searchList[value] : null;
                                 var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                                // if (!display) {
                                //     display = __(value.charAt(0).toUpperCase() + value.slice(1));
                                // }
                                var html = '<span class="text">' + (icon ? '<i class="' + icon + '"></i> ' : '') + value + '</span>';
                                // if (this.operate != false) {
                                //     html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
                                // }
                                return html;
                            }},
                        {field: 'task_sno', title: __('Task_sno')},
                        {field: 'n.username', title: __('商家名称'),operate:false},
                        {field: 'shop_name', title: __('Shop_name'),operate:false},
                        {field: 'real_money', title: __('Service_change_price'),operate:false},
                        {field: 'beat_price', title: __('Beat_price'),operate:false},
                        {field: 'r.username', title: __('Paike_name'),operate:false},
                        {field: 'o.tao_bao_no', title: __('Tao_bao_no')},
                        {field: 'o.tao_bao_order_no', title:__('Tao_bao_order_no'),operate:"LIKE"},
                        {field: 'o.create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'o.task_type', title: __('Task_type'),searchList: {
                                "1":__('精刷单'),
                                "2":__('隔日单'),
                                "3":__('流量单')
                            },formatter:function(value,row,index){
                                var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                                var index = keys.indexOf(value.toString());
                                value = value === null ? '' : value.toString();
                                var display = index > -1 ? this.searchList[value] : null;
                                var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                                if (!display) {
                                    display = __(value.charAt(0).toUpperCase() + value.slice(1));
                                }
                                var html = '<span class="text">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';

                                return html;
                            }},
                        {field: 'payed', title: __('Payed'),searchList: {
                                "1":__('否'),
                                "2":__('是')
                            },formatter: Table.api.formatter.normal},
                        {field: 'is_cancel', title: __('是否取消'),searchList: {
                                "1":__('否'),
                                "2":__('是')
                            },formatter: Table.api.formatter.normal},
                        {field: 'o.status', title: __('Status'),searchList: {

                                "0":__('已拍下'),
                                "1":__('未审核'),
                                "2":__('已审核'),
                                "3":__('已上传'),
                                "4":__('已复查'),
                                "5":__('已付款'),
                                "6":__('已下单'),
                                "7":__('待发货'),
                                "8":__('待好评'),
                                "9":__('已发货'),
                                "10":__('已好评'),
                                "11":__('待追评'),
                                "12":__('已追评'),
                                "13":__('已完成'),
                            },formatter:function(value,row,index){
                                var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                                if(value==undefined || value=="null"){
                                    value =   value
                                }else{
                                    value = value.toString();
                                }
                                var index = keys.indexOf(value);
                                value = value === null ? '' : value;
                                var display = index > -1 ? this.searchList[value] : null;
                                var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                                if (!display) {
                                    display = __(value.charAt(0).toUpperCase() + value.slice(1));
                                }
                                var html = '<span class="text">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';

                                return html;
                            }},
                        {field: 'author_status', title: __('Author_status'), searchList: {"0":__('未审核'),"1":__('已审核'),"2":__('已驳回')},formatter:function(value,row,index){
                                var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                                if(value==undefined || value=="null"){
                                    value =   value
                                }else{
                                    value = value.toString();
                                }
                                var index = keys.indexOf(value);
                                value = value === null ? '' : value;
                                var display = index > -1 ? this.searchList[value] : null;
                                var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                                if (!display) {
                                    display = __(value.charAt(0).toUpperCase() + value.slice(1));
                                }
                                var html = '<span class="text">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';

                                return html;
                            }},
                        {field: 'a.username', title: __('Author'),operate:false},
                        {field: 'order_state', title: __('Order_state'), searchList: {"0":__('正常'),"1":__('已撤销'),"2":__('已回收')},formatter:function(value,row,index){
                                var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                                if(value==undefined || value=="null"){
                                    value =   value
                                }else{
                                    value = value.toString();
                                }
                                var index = keys.indexOf(value);
                                value = value === null ? '' : value;
                                var display = index > -1 ? this.searchList[value] : null;
                                var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                                if (!display) {
                                    display = __(value.charAt(0).toUpperCase() + value.slice(1));
                                }
                                var html = '<span class="text">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';

                                return html;
                            }},
                        {field: 'flag', title: __('是否插件'),searchList: {"1":__('未插旗'),"2":__('已插旗')},formatter: Table.api.formatter.normal},
                        {field: 'flag_remarks', title: __('Flag_remarks'),operate:false},
                        {field: 'room', title: __('Room'),searchList: {"1":__('1'),"2":__('2')},formatter: Table.api.formatter.normal},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter:function(value,row,index) {

                                var table = this.table;
                                // 操作配置
                                var options = table ? table.bootstrapTable('getOptions') : {};
                                // 默认按钮组
                                var buttons = $.extend([], this.buttons || []);

                                var btn_6_text;
                                if(row.order_state == "0" || row.order_state == "2"){
                                    btn_6_text = "撤销";
                                }
                                if(row.order_state == "1"){
                                    btn_6_text = "已撤销";
                                }

                                console.log(row["o.status"]);
                                buttons.push(
                                    {
                                        name: 'btn_1',
                                        text: row.flag == "1" ? __('插旗') : __('已插旗'),
                                        extend: 'data-toggle="tooltip" data-name="btn_1" data-title="插旗"',
                                        classname: 'btn btn-xs btn-success btn-magic btn-small-window'
                                    },
                                    {
                                        name: 'btn_2',
                                        text: "详情",
                                        extend: 'data-toggle="tooltip" data-name="btn_2" data-title="详情"',
                                        classname: 'btn btn-xs btn-success btn-magic btn-editone '
                                    });

                                // if((row["o.status"]!=0 || row["o.status"]!=1 || row["o.status"]!=13) && (row.order_state=="0" || row.order_state=="2") ) {
                                //     buttons.push({
                                //         name: 'btn_6',
                                //         text: btn_6_text,
                                //         extend: 'data-toggle="tooltip" data-name="btn_6" data-title="撤销" ',
                                //         classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                //     })
                                // }
                                console.log(row["o.status"]);
                                if(row["o.status"]!=0 && row["o.status"]!=1 && row["o.status"]!=13) {
                                    if(row.order_state=="0" || row.order_state=="2") {
                                        buttons.push({
                                            name: 'btn_6',
                                            text: btn_6_text,
                                            extend: 'data-toggle="tooltip" data-name="btn_6" data-title="撤销" ',
                                            classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                        })
                                    }
                                }
                                if(row.order_state == "1") {
                                    buttons.push({
                                        name: 'btn_7',
                                        text: "回收",
                                        extend: 'data-toggle="tooltip" data-name="btn_7" data-title="回收" ',
                                        classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    });
                                }
                                if(row.order_state != "1" && row.author_status=="0") {
                                    //审核,驳回:status:(可审核)1,3,6,10,12
                                    if (row["o.status"] == 1 || row["o.status"] == 3 || row["o.status"] == 6 || row["o.status"] == 10 || row["o.status"] == 12) {
                                        buttons.push({
                                                name: 'btn_3',
                                                text: "审核",
                                                extend: 'data-toggle="tooltip" data-name="btn_3"',
                                                classname: 'btn btn-xs btn-success btn-magic btn-ajax '
                                            },
                                            {
                                                name: 'btn_4',
                                                text: "驳回",
                                                extend: 'data-toggle="tooltip" data-name="btn_4" data-title="驳回" ',
                                                classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                            });
                                    }
                                }
                                if(row["o.status"]==5){
                                    buttons.push(  {
                                        name: 'btn_5',
                                        text: "催付",
                                        extend: 'data-toggle="tooltip" data-name="btn_5" data-title="催付" ',
                                        classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    });
                                }


                                return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
                            }
                        }
                    ]
                ]
            });



            // 为表格绑定事件
            Table.api.bindevent(table);

            //批量撤销
            $(document).on("click", "#mult_cancel", function (e){
                var data = table.bootstrapTable('getSelections');
                var table_select = JSON.stringify(table.bootstrapTable('getSelections'));
                layer.confirm('确定全部撤销？',{
                        btn: ['确定','取消'] //可以无限个按钮
                    },function(index, layero){
                        Fast.api.ajax({
                            url: 'order/order/mult_cancel',
                            data: {
                                'ids': data,

                            },
                            success: function (data) {
                                layer.msg(data.msg);

                                table.bootstrapTable("refresh");
                            },
                            error: function (data) {
                                //失败的回调
                                layer.msg(data.msg);
                                table.bootstrapTable("refresh");
                            }
                        });
                    },function(index, layero){
                        layer.msg('已取消操作。');
                    }
                );
            });


            //批量回收
            $(document).on("click", "#mult_back", function (e){
                var data = table.bootstrapTable('getSelections');
                console.log(data);
                var table_select = JSON.stringify(table.bootstrapTable('getSelections'));
                layer.confirm('确定全部回收？',{
                        btn: ['确定','取消'] //可以无限个按钮
                    },function(index, layero){
                        Fast.api.ajax({
                            url: 'order/order/mult_back',
                            data: {
                                'ids': data,

                            },
                            success: function (data) {
                                layer.msg(data.msg);
                                table.bootstrapTable("refresh");
                            },
                            error: function (data) {
                                //失败的回调
                                layer.msg(data.msg);
                                table.bootstrapTable("refresh");
                            }
                        });
                    },function(index, layero){
                        layer.msg('已取消操作。');
                    }
                );
            });



            //批量催款
            $(document).on("click", "#mult_pay", function (e){
                var data = table.bootstrapTable('getSelections');
                console.log(data);
                var table_select = JSON.stringify(table.bootstrapTable('getSelections'));
                layer.confirm('确定全部批量催付？',{
                        btn: ['确定','取消'] //可以无限个按钮
                    },function(index, layero){
                        Fast.api.ajax({
                            url: 'order/order/mult_pay',
                            data: {
                                'ids': data,

                            },
                            success: function (data) {
                                layer.msg(data.msg);
                                table.bootstrapTable("refresh");
                            },
                            error: function (data) {
                                //失败的回调
                                layer.msg(data.msg);
                                table.bootstrapTable("refresh");
                            }
                        });
                    },function(index, layero){
                        layer.msg('已取消操作。');
                    }
                );
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            $(function(){
                //console.log($(".example2"))
                require(['img_css/easy-loading','img_css/hammer.min','img_css/jquery.mousewheel.min','img_css/zoom-marker'],function(){
                    // $('#zoom-marker-img').zoomMarker({
                    //     src: $('#zoom-marker-img').data("url"),
                    //     rate: 0.2,
                    //     width: 100,
                    //     max: 1000,
                    //     markers:[
                    //         //{src:"img/marker.svg", x:500, y:500}
                    //     ]
                    // });
                    $("img").each(function(){
                        console.log($(this).data("url"));
                        if($(this).data("url")!=undefined && $(this).data("url")!="") {
                            $(this).zoomMarker({
                                src: $(this).data("url"),
                                rate: 0.1,
                                min: 100,
                                width: 100,
                                max: 3000,
                                markers: [
                                    //{src:"img/marker.svg", x:500, y:500}
                                ],
                            });
                        }
                    });
                    // $('#zoom-marker-img-alt').zoomMarker({
                    //     src: $('#zoom-marker-img-alt').data("url"),
                    //     rate: 0.1,
                    //     min:100,
                    //     width: 100,
                    //     max: 3000,
                    //     markers:[
                    //         //{src:"img/marker.svg", x:500, y:500}
                    //     ]
                    //
                    //
                    // });
                })
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});